package com.sundaying.gooway.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sundaying.gooway.interceptor.Authority;
import com.sundaying.gooway.model.Json;
import com.sundaying.gooway.model.User;
import com.sundaying.gooway.service.UserService;
import com.sundaying.gooway.util.CipherUtil;

public class UserAction extends ActionSupport {
	private Json json = new Json();
	private UserService userService = new UserService();
	private List<User> userList;
	private User user;
	private String userName;
	private String certCode; // 验证码

	public String getCertCode() {
		return certCode;
	}

	public void setCertCode(String certCode) {
		this.certCode = certCode;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public Json getJson() {
		return json;
	}

	public void setJson(Json json) {
		this.json = json;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Authority(actionName = "UserAction", privilege = "login")
	public String login() {
		System.out.println("登录中...");

		// 验证验证码
		Map session = ActionContext.getContext().getSession();
		String certCode = session.get("certCode").toString();
		if (!certCode.equals(this.certCode)) {
			json.setSuccess(false);
			json.setMsg("请输入正确的验证码");
			return this.SUCCESS;
		}
		
		// 验证用户名和登录密码
		if (userService.verify(user)) {
			json.setSuccess(true);
			json.setMsg("登录成功，请稍候...");
			json.setObj(user);
			// 登录成功，将用户信息增加到session
			session.put("user", user);
		} else {
			json.setSuccess(false);
			json.setMsg("请输入正确的用户名和密码");
			json.setObj(null);
		}

		return this.SUCCESS;
	}
	
	/*
	 * 用户退出系统
	 */
	public String logout(){
		Map session = ActionContext.getContext().getSession();
		session.remove("user");
		return this.SUCCESS;
	}

	public String list() {
		userList = userService.list();
		return this.SUCCESS;
	}

	public String add() {
		// 保存新用户
		userService.add(user);
		return this.SUCCESS;
	}

	public String update() {
		// 密码加密
		// user.setPassword(CipherUtil.encodeByMD5(user.getPassword()));
		userService.update(user);
		return this.SUCCESS;
	}
	
	public String updateState(){
		return this.SUCCESS;
	}

	public String delete() {
		userService.delete(userName);
		return this.SUCCESS;
	}

	@Override
	public void validate() {
		// TODO Auto-generated method stub
		// if (null == certCode || "".equals(certCode)) {
		// this.addFieldError("certCode", "验证码不能为空!");
		// } else {
		// Map session = ActionContext.getContext().getSession();
		// String certCode = session.get("certCode").toString();
		// if (!certCode.equals(this.certCode)) {
		// this.addFieldError("certCode", "验证码不正确!");
		// }
		// }
	}

}
